var t = require("../../../@babel/runtime/helpers/defineProperty"), a = getApp();

Page({
    data: {
        startDate: new Date("1900-01-01"),
        uploadUrl: "upload.uploadPic",
        activity_id: "",
        types: {
            88: "diyusername",
            99: "diypassword",
            0: "diyinput",
            1: "diytextarea",
            3: "diycheckbox",
            4: "diyradio",
            2: "diyselect",
            7: "diydate",
            9: "diycity",
            5: "diyimage"
        },
        thumb: "",
        conference: {},
        form_id: "",
        diyform: {},
        diydata: [],
        intro: !1,
        provinceData: [],
        cityData: [],
        districtData: [],
        pickerValue: [ 0, 0, 0 ],
        selectAddressData: [],
        currentDate: new Date().getTime(),
        isValidation: !0,
        nosubmit: !0,
        is_enrol: 0
    },
    onLoad: function(t) {
        var a = decodeURIComponent(t.scene);
        if (a) {
            var e = [];
            e = a.split(",");
            for (var i = 0; i < e.length; i++) {
                var s;
                "id" == (s = e[i].split("="))[0] && this.setData({
                    activity_id: s[1]
                });
            }
        }
        t.id && this.setData({
            activity_id: t.id
        }), this.data.activity_id || wx.showToast({
            title: "活动不存在！",
            icon: "none",
            duration: 1500,
            success: function() {
                setTimeout(function() {
                    wx.redirectTo({
                        url: "/packageA/member/conferenceList/conferenceList"
                    });
                }, 1500);
            }
        }), this._initAddressInfo(), this.getActivity();
    },
    onRead_1: function(t) {
        try {
            var e = this.data.diydata, i = t.currentTarget.dataset.idex;
            if ("diyimage" == e[i].type && e[i].imgUrls.length >= e[i].data.tp_max) return void wx.showToast({
                title: "最多只能上传".concat(e[i].data.tp_max, "张"),
                icon: "none",
                duration: 1e3
            });
        } catch (t) {
            console.log(t);
        }
        var s = t.currentTarget.dataset.name;
        this.setData({
            setname: s
        });
        var d = this, n = a.getNetAddresss(this.data.uploadUrl);
        wx.chooseImage({
            count: 1,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(t) {
                var a = t.tempFilePaths;
                wx.compressImage({
                    src: a[0],
                    quality: 60,
                    success: function(t) {
                        var a = t.tempFilePath;
                        wx.uploadFile({
                            url: n,
                            filePath: a,
                            name: "file",
                            formData: {
                                attach: d.data.setname
                            },
                            success: function(t) {
                                var a = JSON.parse(t.data);
                                if (console.log(a), 1 == a.result) {
                                    var e = d.data.diydata;
                                    console.log(a);
                                    for (var i = 0; i < e.length; i++) e[i].name == a.data.attach && e[i].imgUrls.push(a.data.img_url);
                                    d.setData({
                                        diydata: e
                                    }), console.log(d.data.diydata);
                                } else wx.showToast({
                                    icon: "none",
                                    title: a.msg,
                                    duration: 1500
                                });
                            }
                        });
                    },
                    fail: function(t) {
                        console.log(t);
                    },
                    complete: function(t) {}
                });
            }
        });
    },
    removeImg: function(t) {
        var a = t.currentTarget.dataset.indexc, e = t.currentTarget.dataset.index, i = this.data.diydata;
        i[e].imgUrls.splice(a, 1), this.setData({
            diydata: i
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        return {
            title: this.data.conference.title ? this.data.conference.title : "活动",
            path: "/packageB/conference/activity/activity?id=" + this.data.activity_id
        };
    },
    getActivity: function() {
        var t = this, e = a.getNetAddresss("plugin.conference.api.conference-activity.get-activity-detail");
        e += "&id=" + this.data.activity_id, a._getNetWork({
            url: e,
            success: function(a) {
                var e = a.data;
                if (1 == e.result) {
                    if (1 == e.data.is_end) return void wx.showToast({
                        title: "来晚了,活动已结束！",
                        icon: "none",
                        duration: 1500,
                        success: function() {
                            setTimeout(function() {
                                wx.redirectTo({
                                    url: "/packageA/member/conferenceList/conferenceList"
                                });
                            }, 1500);
                        }
                    });
                    if (t.setData({
                        thumb: e.data.thumb,
                        conference: e.data,
                        form_id: e.data.form_id
                    }), wx.setNavigationBarTitle({
                        title: t.data.conference.title ? t.data.conference.title : "活动"
                    }), t.getForm(), 1 == e.data.is_enrol) return t.setData({
                        is_enrol: e.data.is_enrol
                    }), void wx.showToast({
                        title: "您已报名该活动！",
                        icon: "none"
                    });
                } else wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    goDetail: function() {
        wx.navigateTo({
            url: "/packageB/conference/myActivityInfo/myActivityInfo?id=" + this.data.activity_id
        });
    },
    getForm: function() {
        var t = this, e = a.getNetAddresss("plugin.diyform.api.diy-form.get-diy-form-by-id");
        e += "&form_id=" + this.data.form_id, a._getNetWork({
            url: e,
            success: function(a) {
                var e = a.data, i = {};
                if (1 == e.result) {
                    t.setData({
                        diyform: e.data
                    });
                    var s = t.data.diyform.fields, d = t.data.diydata;
                    for (var n in s) {
                        if ("diycheckbox" == t.data.types[s[n].data_type]) i = {
                            name: n,
                            data: s[n],
                            value: [],
                            type: t.data.types[s[n].data_type]
                        }; else if ("diydate" == t.data.types[s[n].data_type]) i = {
                            name: n,
                            data: s[n],
                            value: "",
                            type: t.data.types[s[n].data_type]
                        }; else if ("diyselect" == t.data.types[s[n].data_type]) {
                            for (var o = [], r = 0; r < s[n].tp_text.length; r++) o.push({
                                name: s[n].tp_text[r]
                            });
                            i = {
                                name: n,
                                data: s[n],
                                value: "",
                                type: t.data.types[s[n].data_type],
                                diysishow: !1,
                                sitem: o
                            };
                        } else i = "diycity" == t.data.types[s[n].data_type] ? {
                            name: n,
                            data: s[n],
                            value: "",
                            type: t.data.types[s[n].data_type],
                            showAdd: !1
                        } : "diyimage" == t.data.types[s[n].data_type] ? {
                            name: n,
                            data: s[n],
                            imgUrls: s[n].value ? s[n].value : [],
                            isPhoto: !1,
                            type: t.data.types[s[n].data_type]
                        } : {
                            name: n,
                            data: s[n],
                            value: "",
                            type: t.data.types[s[n].data_type]
                        };
                        d.push(i), t.setData({
                            diydata: d
                        }), console.log(t.data.diydata);
                    }
                }
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    showIntro: function() {
        this.setData({
            intro: !0
        });
    },
    showIntroClose: function() {
        this.setData({
            intro: !1
        });
    },
    tpnameinp: function(a) {
        var e = a.detail.value, i = a.currentTarget.dataset.idex;
        this.setData(t({}, "diydata[" + i + "].value", e));
    },
    diytextareainp: function(a) {
        var e = a.detail.value, i = a.currentTarget.dataset.idex;
        this.setData(t({}, "diydata[" + i + "].value", e));
    },
    checkboxinp: function(a) {
        var e = a.detail, i = a.currentTarget.dataset.idex;
        this.setData(t({}, "diydata[" + i + "].value", e));
    },
    diyselectBtn: function(a) {
        var e = a.currentTarget.dataset.idex;
        this.setData(t({}, "diydata[" + e + "].diysishow", !0));
    },
    sitemClose: function(a) {
        var e = a.currentTarget.dataset.idex;
        this.setData(t({}, "diydata[" + e + "].diysishow", !1));
    },
    sitemSelectBtn: function(a) {
        var e, i = a.currentTarget.dataset.idex, s = a.detail.name;
        this.setData((t(e = {}, "diydata[" + i + "].value", s), t(e, "diydata[" + i + "].diysishow", !1), 
        e));
    },
    raidinp: function(a) {
        var e = a.detail, i = a.currentTarget.dataset.idex;
        this.setData(t({}, "diydata[" + i + "].value", e));
    },
    _closeDateLw: function(a) {
        var e = a.currentTarget.dataset.idex;
        this.setData(t({}, "diydata[" + e + "].showAdd", !1));
    },
    _initAddressInfo: function() {
        var t = this;
        wx.getStorage({
            key: "provinceData",
            success: function(a) {
                console.log("地址信息准备就绪"), t._getProvinceData();
            },
            fail: function(e) {
                console.log("省fail"), a._getNetWork({
                    url: a.getNetAddresss("member.member-address.address"),
                    success: function(a) {
                        var e = a.data, i = e.data.province, s = e.data.city, d = e.data.district;
                        wx.setStorage({
                            key: "provinceData",
                            data: i
                        }), wx.setStorage({
                            key: "cityData",
                            data: s
                        }), wx.setStorage({
                            key: "districtData",
                            data: d
                        }), t._initAddressInfo();
                    },
                    fail: function(t) {},
                    state: !0
                });
            }
        });
    },
    _getProvinceData: function() {
        var t = this;
        wx.getStorage({
            key: "provinceData",
            success: function(a) {
                console.log("省读取成功", a.data), t.setData({
                    provinceData: a.data
                }), t.setData({
                    "selectAddressData[0].name": a.data[t.data.pickerValue[0]].areaname,
                    "selectAddressData[0].id": a.data[t.data.pickerValue[0]].id,
                    "selectAddressData[0].index": t.data.pickerValue[0]
                }), t._getCityData();
            }
        });
    },
    _getCityData: function() {
        var t = this.data.selectAddressData[0];
        console.log("城市", "_data", t);
        var a = this;
        wx.getStorage({
            key: "cityData",
            success: function(e) {
                console.log("城市读取成功", e);
                for (var i = [], s = 0; s < e.data.length; s++) e.data[s].parentid == t.id && i.push(e.data[s]);
                console.log("城市数据筛选", i), a.setData({
                    cityData: i
                }), a.setData({
                    "selectAddressData[1].name": i[a.data.pickerValue[1]].areaname,
                    "selectAddressData[1].id": i[a.data.pickerValue[1]].id,
                    "selectAddressData[1].index": a.data.pickerValue[1]
                }), a._getDistrictData();
            }
        });
    },
    _getDistrictData: function() {
        var t = this.data.selectAddressData[1];
        console.log("地区", "_data", t), console.log(t);
        var a = this;
        wx.getStorage({
            key: "districtData",
            success: function(e) {
                console.log("地区读取成功");
                for (var i = [], s = 0; s < e.data.length; s++) e.data[s].parentid == t.id && i.push(e.data[s]);
                console.log("地区数据筛选", i), a.setData({
                    districtData: i
                }), console.log("districtData", i), a.setData({
                    "selectAddressData[2].name": i[a.data.pickerValue[2]].areaname,
                    "selectAddressData[2].id": i[a.data.pickerValue[2]].id,
                    "selectAddressData[2].index": a.data.pickerValue[2]
                }), console.log("over", a.data.selectAddressData);
            }
        });
    },
    bindChange: function(t) {
        var a = t.detail.value;
        this.data.pickerValue[0] != a[0] && (a[1] = 0, a[2] = 0), this.data.pickerValue[1] != a[1] && (a[2] = 0), 
        this.setData({
            "pickerValue[0]": a[0],
            "pickerValue[1]": a[1],
            "pickerValue[2]": a[2]
        }), this._initAddressInfo();
    },
    resultAdd: function(a) {
        var e, i = a.currentTarget.dataset.idex, s = this.data.selectAddressData, d = s[0].name + " " + s[1].name + " " + s[2].name;
        this.setData((t(e = {}, "diydata[" + i + "].value", d), t(e, "diydata[" + i + "].showAdd", !1), 
        e));
    },
    openPicker: function(a) {
        var e = a.currentTarget.dataset.idex;
        this.setData(t({}, "diydata[" + e + "].timeShow", !0));
    },
    PickerClose: function(a) {
        var e = a.currentTarget.dataset.idex;
        this.setData(t({}, "diydata[" + e + "].timeShow", !1));
    },
    PickerChange: function(a) {
        var e = a.currentTarget.dataset.idex, i = a.detail;
        this.setData(t({}, "diydata[" + e + "].currentDate", i));
    },
    birthdayconfirm: function(a) {
        var e, i = a.currentTarget.dataset.idex, s = a.detail, d = this.timestampToTime(s);
        this.setData((t(e = {}, "diydata[" + i + "].value", d), t(e, "diydata[" + i + "].timeShow", !1), 
        e));
    },
    timestampToTime: function(t) {
        var a = new Date(t);
        return a.getFullYear() + "-" + ((a.getMonth() + 1 < 10 ? "0" + (a.getMonth() + 1) : a.getMonth() + 1) + "-") + a.getDate();
    },
    submit: function() {
        this.selectComponent(".diyfrom").submit();
    },
    dfsave: function(t) {
        this.setData({
            formData: t.detail
        }), this.submitConference();
    },
    submitConference: function() {
        var t = this, e = a.getNetAddresss("plugin.conference.api.conference-activity.save-enrol");
        e += "&conference_id=" + this.data.activity_id, a._getNetWork({
            url: e,
            success: function(a) {
                var e = a.data;
                if (1 == e.result) {
                    var i = e.data.enrol_id;
                    t.submitForm(i);
                } else wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                }), wx.redirectTo({
                    url: "/packageA/member/conferenceList/conferenceList"
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    submitForm: function(t) {
        var e = this, i = {};
        this.data.diydata.forEach(function(t) {
            "diyimage" == t.type ? i[t.name] = t.imgUrls : i[t.name] = t.value;
        }), console.log(i, this.data.diydata), [].push(i);
        var s = {
            form_data: this.data.formData,
            form_id: this.data.form_id
        }, d = a.getNetAddresss("plugin.diyform.api.diy-form.save-diy-form-data");
        a._postNetWork({
            url: d,
            showToastIn: !1,
            data: s,
            success: function(a) {
                var i = a.data;
                if (1 == i.result) {
                    var s = i.data.form_data_id;
                    e.submitEnro(t, s);
                } else wx.showToast({
                    icon: "none",
                    title: i.msg,
                    duration: 1500
                });
            },
            fail: function(t) {}
        });
    },
    submitEnro: function(t, e) {
        var i = a.getNetAddresss("plugin.conference.api.conference-activity.save-enrol-end"), s = {
            enrol_id: t,
            form_data_id: e
        };
        a._postNetWork({
            url: i,
            showToastIn: !1,
            data: s,
            success: function(t) {
                1 == t.data.result && wx.showToast({
                    title: "报名成功",
                    icon: "none",
                    duration: 1500,
                    success: function() {
                        setTimeout(function() {
                            wx.redirectTo({
                                url: "/packageA/member/conferenceList/conferenceList"
                            });
                        }, 1500);
                    }
                });
            },
            fail: function(t) {}
        });
    },
    showAddBtn: function(a) {
        var e = a.currentTarget.dataset.idex;
        this.setData(t({}, "diydata[" + e + "].showAdd", !0));
    }
});